/** 
 * Date:2014年8月7日下午4:40:24 
 * Copyright (c) 2014, YIMAFU Co., Ltd. All right.
 * Author: hujl@weishequan.com
 */
package cn.com.qingz.modules.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.core.exception.DicmfException;
import cn.com.qingz.core.model.DefaultResp;
import cn.com.qingz.modules.user.model.User;
import cn.com.qingz.modules.user.service.UserService;
import cn.com.qingz.modules.user.vo.UserCondition;
import cn.com.qingz.modules.user.vo.UserPrivilegeVo;

@Controller
@RequestMapping("user/*")
public class UserController {

    Logger log = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("userManagePage")
    public String userManagePage() {
        return "user/userManage";
    }

    @RequestMapping("setRolePage")
    public String setRolePage() {
        return "user/setRole";
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping("regist")
    @ResponseBody
    public DefaultResp<String> regist(User user) {
        DefaultResp<String> res = new DefaultResp<String>();
        userService.doRegister(user);
        return res;
    }

    @RequestMapping("updateUserInfo")
    @ResponseBody
    public DefaultResp<String> updateUserInfo(User user, Long userid) {
        DefaultResp<String> res = new DefaultResp<String>();
        user.setId(userid);
        userService.doUpdateUserInfo(user);
        return res;
    }

    /**
     * 修改密码
     * @param user
     * @param newPwd
     * @return
     */
    @RequestMapping("changePwd")
    @ResponseBody
    public DefaultResp<String> changePwd(User user, String newPwd) {
        DefaultResp<String> res = new DefaultResp<String>();
        userService.doUpdatePassword(user, newPwd);
        return res;
    }

    //查询系统用户
    @RequestMapping("querySysUser")
    @ResponseBody
    public Map<String, Object> querySysUser(UserCondition condition, @RequestParam(required = false, defaultValue = "0") int offset,
            @RequestParam(required = false, defaultValue = "10") int limit) {
        final int pageNo = offset / limit + 1;
        PageList<UserPrivilegeVo> list = userService.queryUserPage(condition, pageNo, limit);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("total", list.getPaginator().getTotalCount());
        res.put("rows", list);

        return res;
    }

    /**
     * 设置用户角色
     * @param userid
     * @param role_ids
     * @return
     */
    @RequestMapping("setRole")
    @ResponseBody
    public DefaultResp<String> setRole(Long userid, String role_ids) {
        DefaultResp<String> res = new DefaultResp<String>();
        userService.doSetRole(userid, role_ids);
        return res;
    }

    @RequestMapping("disableUser")
    @ResponseBody
    public DefaultResp<String> disableUser(String userids) {
        DefaultResp<String> res = new DefaultResp<String>();
        userService.doDisableUsers(userids);
        return res;
    }

    /**
     * 启用 / 禁用
     * @param userid
     * @param role_ids
     * @return
     */
    @RequestMapping("onOff")
    @ResponseBody
    public DefaultResp<String> onOff(Long userid, String status) {
        DefaultResp<String> res = new DefaultResp<String>();
        if ("Y".equals(status) || "N".equals(status)) {
            userService.doOnOff(userid, status);
        } else {
            throw new DicmfException(SystemConstantes.ERROR, "status 参数值错误 Y/N");
        }
        return res;
    }

    @RequestMapping("test")
    @ResponseBody
    public List<Map<String, Object>> test() {
        return userService.test();
    }

}
